package com.cupcorn.lib.display;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.entity.scene.Scene;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.util.adt.io.in.IInputStreamOpener;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.cupcorn.lib.Utils;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameSprite;
import com.cupcorn.lib.game.element.GameElementSprite;

public class DisplaySprite
{
	GameEngine			engine;
	GameElementSprite[]	gameElementSprite;
	ITextureRegion[]	textureRegion;
	GameSprite[]		sprite;
	
	public DisplaySprite(final GameEngine engine, GameElementSprite[] gameElementSprite)
	{
		this.engine = engine;
		this.gameElementSprite = gameElementSprite;
		
		textureRegion = new ITextureRegion[gameElementSprite.length];
		sprite = new GameSprite[gameElementSprite.length];
		
		for (int i = 0; i < gameElementSprite.length; i++)
		{
			final String path = gameElementSprite[i].path;
			try
			{
				ITexture textureAtlas = new BitmapTexture(engine.getTextureManager(), new IInputStreamOpener()
				{
					public InputStream open() throws IOException
					{
						return engine.getAssets().open(path);
					}
				});
				
				textureAtlas.load();
				textureRegion[i] = TextureRegionFactory.extractFromTexture(textureAtlas);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public GameSprite newSprite(int index)
	{
		return newSprite(index, engine.getEngine().getScene(), true);
	}
	
	public GameSprite newSprite(int index, Scene scene)
	{
		return newSprite(index, scene, true);
	}
	
	public GameSprite newSprite(int index, Scene scene, boolean autoAttach)
	{
		GameSprite sprite = sprite(index);
		
		if(autoAttach)
		{
			scene.attachChild(sprite);
		}
		else
		{
			
		}
		
		if (gameElementSprite[index].isAreaTouched) scene.registerTouchArea(sprite);
		return sprite;
	}
	
	public GameSprite sprite(int index)
	{
		float width = 0;
		float height = 0;
		
		if (gameElementSprite[index].width != 0 && gameElementSprite[index].height != 0)
		{
			width = Utils.getRatioWidth(gameElementSprite[index].width, gameElementSprite[index].ratio);
			height = Utils.getRatioHeight(gameElementSprite[index].height, gameElementSprite[index].ratio);
		}
		else
		{
			try
			{
				Bitmap bitmap = BitmapFactory.decodeStream(engine.getAssets().open(gameElementSprite[index].path));
				width = Utils.getRatioWidth(bitmap.getWidth(), gameElementSprite[index].ratio);
				height = Utils.getRatioHeight(bitmap.getHeight(), gameElementSprite[index].ratio);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		float x = Utils.getPosX(gameElementSprite[index].x, width, gameElementSprite[index].anchor, gameElementSprite[index].ratio);
		float y = Utils.getPosY(gameElementSprite[index].y, height, gameElementSprite[index].anchor, gameElementSprite[index].ratio);
		sprite[index] = new GameSprite(x, y, width, height, textureRegion[index], engine.getVertexBufferObjectManager(), gameElementSprite[index].ratio);
		return sprite[index];
	}
	
	public GameSprite getSprite(int index)
	{
		return sprite[index];
	}
}
